<template>
	<view class="page">
		<uni-nav-bar :fixed="true" left-icon="back" :border="false" backgroundColor="#f9f2ea" leftText="优惠券"
			:statusBar="true" @clickLeft="goBack"></uni-nav-bar>
		<view class="nav">
			<view class="nav-box" @click="navChange('0')">
				<view class="nav-item" :class="{'active': navChoose == 0}">
					活动领券
				</view>
			</view>
			<view class="nav-box" @click="navChange('1')">
				<view class="nav-item" :class="{'active': navChoose == 1}">
					我的券包
				</view>
			</view>
		</view>
		<view class="search">
			<image class="img" src="../../static/search.png" mode=""></image>
			<input class="input" type="text" v-model="code" placeholder="输入兑换码"
				placeholder-style="font-size: 24rpx;color: #ccc" value="" />
			<!-- <input class="input" type="text" v-model="keywords" placeholder="" placeholder-style="font-size: 24rpx;color: #ccc" value="" /> -->
			<view class="delete" v-if="code!=''">
				<image class="img" src="../../static/search_delete.png" mode="" @click="deleteSearch"></image>
			</view>
			<view class="search-line"></view>
			<view class="search-btn" @click="getList">
				搜索
			</view>
		</view>
		<view class="cont" v-show="navChoose == 0">
			<view class="box" v-if="couponList1.length > 0">
				<view class="title">
					新人优惠券
				</view>
				<view class="coupon-box">
					<view class="coupon-item flex" :class="{'receive': item.is_receive == 2}" @click="doReceive(item)"
						v-for="(item, index) in couponList1" :key="index">
						<view class="">
							<view class="flex">
								<view class="price">
									￥{{parseFloat(item.discount_price)}}
								</view>
								<view class="text">
									满{{parseFloat(item.total_price)}}可用
								</view>
							</view>
							<view class="time">
								有效期为自领用之日起{{item.expire}}天
							</view>
						</view>
						<!-- 	<view class="btn">
							立即领取
						</view> -->
					</view>
				</view>
			</view>
			<view class="box" v-if="couponList2.length > 0">
				<view class="title">
					游戏通关券
				</view>
				<view class="coupon-box">
					<view class="coupon-item flex" v-for="(item, index) in couponList2" :key="index">
						<view class="">
							<view class="flex">
								<view class="price">
									￥{{parseFloat(item.discount_price)}}
								</view>
								<view class="text">
									满{{parseFloat(item.total_price)}}可用
								</view>
							</view>
							<view class="time">
								有效期为自领用之日起{{item.expire}}天
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="box" v-if="couponList3.length > 0">
				<view class="title">
					免费领取券
				</view>
				<view class="coupon-box">
					<view class="coupon-item flex" :class="{'receive': item.is_receive == 2}" @click="doReceive(item)"
						v-for="(item, index) in couponList3" :key="index">
						<view class="">
							<view class="flex">
								<view class="price">
									￥{{parseFloat(item.discount_price)}}
								</view>
								<view class="text">
									满{{parseFloat(item.total_price)}}可用
								</view>
							</view>
							<view class="time">
								有效期为自领用之日起{{item.expire}}天
							</view>
						</view>
						<view class="btn">
							立即领取
						</view>
					</view>
				</view>
			</view>
			<view class="box" v-if="couponList5.length > 0">
				<view class="title">
					兑换券
				</view>
				<view class="coupon-box">
					<view class="coupon-item flex" :class="{'receive': item.is_receive == 2}" @click="doReceive(item)"
						v-for="(item, index) in couponList5" :key="index">
						<view class="">
							<view class="flex">
								<view class="price">
									￥{{parseFloat(item.discount_price)}}
								</view>
								<view class="text">
									满{{parseFloat(item.total_price)}}可用
								</view>
							</view>
							<view class="time">
								有效期为自领用之日起{{item.expire}}天<br /> <!-- 设计师：{{item.designer}} -->
								仅限包含<text style="color:#a64e4e;margin:0 10rpx;"> {{item.designer}} </text>设计的商品使用

							</view>
						</view>
						<view class="btn">
							立即领取
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="cont" v-show="navChoose == 1">
			<view class="box" v-if="couponList4.length > 0">
				<view class="coupon-box">
					<view class="coupon-item flex" v-for="(item, index) in couponList4" :key="index">
						<view class="">
							<view class="flex">
								<view class="price">
									￥{{parseFloat(item.discount_price)}}
								</view>
								<view class="text">
									满{{parseFloat(item.total_price)}}可用
								</view>
							</view>
							<view class="time">
								到期日{{item.expire_time}}<br />
								<text v-if="item.designer==''">定制、商铺商品可使用</text>
								<text v-if="item.designer">
									仅限包含<text style="color:#a64e4e;"> {{item.designer}} </text>设计的商品使用
								</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		couponList,
		receiveCoupon,
		userCoupon
	} from "@/api/public.js"
	export default {
		data() {
			return {
				navChoose: 0,
				page: 1,
				couponList1: [],
				couponList2: [],
				couponList3: [],
				couponList4: [],
				couponList5: [],
				code: ''
			}
		},
		onShow() {
			this.getList()
		},
		methods: {
			toSearch() { //领取优惠券
				// this.page = 1;
				// this.orderArr = [];
				// this.getList();
			},
			deleteSearch() { //删除
				this.code = "";
				console.log(this.keywords)
			},
			goBack() {
				uni.navigateBack();
			},
			navChange(type) {
				this.navChoose = type;
				if (type == 0) {
					this.getList();
				} else {
					this.getMine()
				}
			},
			getMine() {
				userCoupon(this.page, 1).then(res => {
					this.couponList4 = res.reData.data;
				})
			},
			getList() {
				couponList(this.page, this.code).then(res => { //y优惠券列表
					this.couponList1 = [];
					this.couponList2 = [];
					this.couponList3 = [];
					this.couponList5 = [];
					for (let i in res.reData.data) {
						if (res.reData.data[i].type == 1) {
							this.couponList1.push(res.reData.data[i])
						}
						if (res.reData.data[i].type == 2) {
							this.couponList2.push(res.reData.data[i])
						}
						if (res.reData.data[i].type == 3) {
							this.couponList3.push(res.reData.data[i])
						}
						if (res.reData.data[i].type == 4) {
							this.couponList5.push(res.reData.data[i])
						}


					}
				})
			},

			doReceive(item) {
				if (item.is_receive == 2) {
					uni.showToast({
						title: "已领取",
						icon: "none"
					})
				} else {
					receiveCoupon(item.id).then(res => {
						if (res.result == 0) {
							uni.showToast({
								title: "领取成功",
								icon: "none"
							})
							item.is_receive = 2
						}
					})
				}
			},
		},
	}
</script>

<style lang="scss" scoped>
	.search-mark {
		margin-top: 30rpx;
		margin-bottom: 30rpx;
		width: 690rpx;
		height: 170rpx;
	}

	.search-box {
		position: fixed;
		z-index: 1000;
		left: 0;
		right: 0;
		padding: 30rpx;
		background: #f9f2ea;
	}

	.search {
		margin: auto;
		margin-top: 100rpx;
		box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.07);
		width: 660rpx;
		height: 80rpx;
		background: #fffaf5;
		border-radius: 40rpx;
		padding-left: 30rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 30rpx;

		.img {
			position: absolute;
			width: 40rpx;
			height: 40rpx;
			margin-right: 30rpx;
		}

		.input {
			width: 480rpx;
			padding-left: 70rpx;
		}

		.delete {
			position: absolute;
			right: 140rpx;
			width: 40rpx;
			height: 40rpx;
			z-index: 10000;

			.img {
				width: 40rpx;
				height: 40rpx;
			}
		}

		.search-line {
			height: 30rpx;
			width: 2rpx;
			background-color: #e9e9e9;
		}

		.search-btn {
			width: 100rpx;
			text-align: center;
			font-size: 24rpx;
			line-height: 80rpx;
			margin-right: 20rpx;
		}
	}

	.page {
		position: fixed;
		width: 100%;
		height: 100%;
		top: 0;
		left: 0;
		overflow: auto;

		.cont {}

	}

	.nav {
		position: fixed;
		width: 100%;
		display: flex;
		justify-content: space-around;
		background: #f9f2ea;

		.nav-box {
			width: 50%;
		}

		.nav-item {
			box-sizing: border-box;
			width: 150rpx;
			margin: 0 auto;
			font-size: 32rpx;
			text-align: center;
			line-height: 94rpx;

			&.active {
				color: rgba(207, 126, 109, 1);
			}
		}
	}

	.title {
		font-size: 32rpx;
		line-height: 80rpx;
		color: #bd5b5a;
		padding: 0 32rpx;
	}

	.box {
		width: 640rpx;
		padding: 20rpx;
		margin: 0 auto 30rpx;
		background: rgba(255, 250, 245, 1);
		border-radius: 20rpx;
	}

	.coupon-box {

		.coupon-item {
			width: 560rpx;
			margin: 0 auto 20rpx;
			height: 160rpx;
			padding: 0 40rpx;
			justify-content: space-between;
			align-items: center;
			background-image: url(../../static/coupon_5.png);
			background-position: center;
			background-size: 100% 100%;
			background-repeat: no-repeat;

			.flex {
				align-items: center;
				color: rgba(166, 78, 78, 1);

				.price {
					font-size: 48rpx;
					line-height: 72rpx;
					margin-right: 40rpx;
				}

				.text {
					font-size: 28rpx;
					line-height: 44rpx;
				}
			}

			.time {
				font-size: 24rpx;
				color: rgba(112, 112, 112, 1);
			}

			.btn {
				width: 160rpx;
				height: 60rpx;
				border-radius: 30rpx;
				border: 2rpx solid rgba(166, 78, 78, 1);
				color: rgba(166, 78, 78, 1);
				line-height: 60rpx;
				text-align: center;
			}

			&.receive {
				.btn {
					display: none;
				}
			}
		}

		// .coupon-item{
		// 	&.used{
		// 		background-image: url(../../static/coupon_3.png);
		// 	}
		// 	&.receive{
		// 		background-image: url(../../static/coupon_2.png);
		// 	}
		// }

		.coupon-item:last-child {
			margin-bottom: 0;
		}
	}
</style>
